package com.excesys.exsecs.scada.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.entity.AccessDeviceInfo;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.scada.entity.ScadaDevice;
import com.excesys.exsecs.scada.entity.ScadaProperty;
import com.excesys.exsecs.scada.service.ScadaDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组态、设备关联表
 *
 * @author lxc
 * @date 2024-05-17 11:58:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/scadaDevice" )
@Tag(description = "scadaDevice" , name = "组态、设备关联表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScadaDeviceController {

    private final  ScadaDeviceService scadaDeviceService;

	/**
	 * 设备分页查询 过滤掉组态已绑定的设备
	 * @param scadaProperty
	 * @param page
	 * @return
	 */
	@GetMapping("/getDeviceList")
	public R getDeviceList(@ParameterObject ScadaProperty scadaProperty,
						   @ParameterObject AccessDeviceInfo accessDeviceInfo,
						   @ParameterObject Page page){
		return R.ok(scadaDeviceService.queryPage(scadaProperty,accessDeviceInfo,page),"设备分页查询");
	}

	/**
	 * 根据组态ID查询已经绑定的设备列表
	 * @param scadaProperty
	 * @param page
	 * @return
	 */
	@GetMapping("/queryByScadaId")
	public R queryByScadaId(@ParameterObject ScadaProperty scadaProperty,
							@ParameterObject AccessDeviceInfo accessDeviceInfo,
							@ParameterObject Page page){
		return R.ok(scadaDeviceService.queryByScadaId(scadaProperty,accessDeviceInfo,page),"根据组态ID查询已经绑定的设备列表");
	}


	/**
     * 分页查询
     * @param page 分页对象
     * @param scadaDevice 组态、设备关联表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('scada_scadaDevice_view')" )
    public R getScadaDevicePage(@ParameterObject Page page, @ParameterObject ScadaDevice scadaDevice) {
        LambdaQueryWrapper<ScadaDevice> wrapper = Wrappers.lambdaQuery();
        return R.ok(scadaDeviceService.page(page, wrapper));
    }


    /**
     * 通过id查询组态、设备关联表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('scada_scadaDevice_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(scadaDeviceService.getById(id));
    }

    /**
     * 新增组态、设备关联表
     * @param scadaDevice 组态、设备关联表
     * @return R
     */
    @Operation(summary = "新增组态、设备关联表" , description = "新增组态、设备关联表" )
    @SysLog("新增组态、设备关联表" )
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('scada_scadaDevice_add')" )
    public R save(@RequestBody ScadaDevice scadaDevice) {
        return R.ok(scadaDeviceService.saveScadaDevice(scadaDevice));
    }

    /**
     * 修改组态、设备关联表
     * @param scadaDevice 组态、设备关联表
     * @return R
     */
    @Operation(summary = "修改组态、设备关联表" , description = "修改组态、设备关联表" )
    @SysLog("修改组态、设备关联表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('scada_scadaDevice_edit')" )
    public R updateById(@RequestBody ScadaDevice scadaDevice) {
        return R.ok(scadaDeviceService.updateById(scadaDevice));
    }

    /**
     * 通过设备id删除组态、设备关联表
     * @param scadaDevice id列表
     * @return R
     */
    @Operation(summary = "通过id删除组态、设备关联表" , description = "通过id删除组态、设备关联表" )
    @SysLog("通过id删除组态、设备关联表" )
    @DeleteMapping
    //@PreAuthorize("@pms.hasPermission('scada_scadaDevice_del')" )
    public R removeById(@RequestBody ScadaDevice scadaDevice) {
        return R.ok(scadaDeviceService.removeBatchByDeviceIds(scadaDevice));
    }


    /**
     * 导出excel 表格
     * @param scadaDevice 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('scada_scadaDevice_export')" )
    public List<ScadaDevice> export(ScadaDevice scadaDevice, Integer[] ids) {
        return scadaDeviceService.list(Wrappers.lambdaQuery(scadaDevice).in(ArrayUtil.isNotEmpty(ids), ScadaDevice::getId, ids));
    }

}